#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../include/sec_demo.h"
#include "../include/pci_dma.h"

int host_card_dma_transmit(int ccp903_fd, void *pdata, unsigned int len)
{
	int ret;

	if(pdata == NULL) {
		printf("%s: pdata is equal with NULL!\n",__func__);
		return -1;
	}
	if(len <= 0) {
		printf("%s: Input data size is equal with zero or nagative!\n",__func__);
		return -1;
	}

	ret = ioctl(ccp903_fd, _IOC(0x00, CCP903T_PCIE_MAGIC, PCI_DMA_TRANSMIT, len), pdata);
	if(ret) {
		printf("%s: ioctl function call is failed!\n",__func__);
		return -1;
	}

	return 0;
}
void pci_dma_test(int ccp903_fd)
{
	char buf[64];
	char ref[64];
	int i;

	memset(buf, 0x15, 64);
	memcpy(ref, buf, 64);
	//for(i=0;i<64;i++)
	//	printf("TX-%02x: %02x\n",i, buf[i]);
	// printf("PCI DMA send data:\n");
	// print_hex_dump(ref, 64);

	host_card_dma_transmit(ccp903_fd, buf, 64);

	// printf("PCI DMA receive data:\n");
	// print_hex_dump(buf, 64);

	if(!memcmp(buf,ref,64)) {
		printf("PCI DMA write and read data successfully!\n");
	}else {
		printf("PCI DMA write and read data is NOT work, failed!\n");
	}
}

